home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / READNEXT.C < prev    next >
C/C++ Source or Header  |  1991-09-28  |  4KB  |  93 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    r e a d n e x t . c                                             */
  3. /*                                                                    */
  4. /*    Reads a spooling directory with optional pattern matching       */
  5. /*                                                                    */
  6. /*    Copyright 1991 (C), Andrew H. Derbyshire                        */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. /*--------------------------------------------------------------------*/
  10. /*                        System include files                        */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16.  
  17. /*--------------------------------------------------------------------*/
  18. /*                    UUPC/extended include files                     */
  19. /*--------------------------------------------------------------------*/
  20.  
  21. #include "lib.h"
  22. #include "readnext.h"
  23. #include "ndir.h"
  24.  
  25. /*--------------------------------------------------------------------*/
  26. /*    r e a d n e x t                                                 */
  27. /*                                                                    */
  28. /*    Read a directory into a linked list                             */
  29. /*--------------------------------------------------------------------*/
  30.  
  31. char     *readnext(char *xname,
  32.           const char *remote,
  33.           const char *subdir,
  34.           char *pattern )
  35. {
  36.    static DIR *dirp;
  37.    static char *saveremote = NULL;
  38.    static char remotedir[FILENAME_MAX];
  39.  
  40.    struct direct *dp;
  41.  
  42. /*--------------------------------------------------------------------*/
  43. /*          Determine if we must restart the directory scan           */
  44. /*--------------------------------------------------------------------*/
  45.  
  46.    if ( (remote == NULL) || (saveremote == NULL ) ||
  47.         !equal(remote, saveremote) )
  48.    {
  49.       if ( saveremote != NULL )  /* Clean up old directory? */
  50.       {                          /* Yes --> Do so           */
  51.          closedir(dirp);
  52.          saveremote = NULL;
  53.       } /* if */
  54.  
  55.       if ( remote == NULL )      /* Clean up only, no new search? */
  56.          return NULL;            /* Yes --> Return to caller      */
  57.  
  58.       if ( pattern == NULL )
  59.          pattern = "*.*";
  60.  
  61.       sprintf(remotedir,"%s/%.8s/%s",spooldir,remote, subdir);
  62.       if ((dirp = opendirx(remotedir,pattern)) == nil(DIR))
  63.       {
  64.          printmsg(2, "readnext: couldn't opendir() %s", remotedir);
  65.          return NULL;
  66.       } /* if */
  67.  
  68.       saveremote = (char *) remote;
  69.                               /* Flag we have an active search    */
  70.    } /* if */
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*              Look for the next file in the directory               */
  74. /*--------------------------------------------------------------------*/
  75.  
  76.    if ((dp = readdir(dirp)) != nil(struct direct))
  77.    {
  78.       sprintf(xname, "%s/%s", remotedir, dp->d_name);
  79.       printmsg(5, "readnext: matched \"%s\"",xname);
  80.       return xname;
  81.    }
  82.  
  83. /*--------------------------------------------------------------------*/
  84. /*     No hit; clean up after ourselves and return to the caller      */
  85. /*--------------------------------------------------------------------*/
  86.  
  87.    printmsg(5, "readnext: \"%s\" not matched", remotedir);
  88.    closedir(dirp);
  89.    saveremote = NULL;
  90.    return NULL;
  91.  
  92. } /*readnext*/
  93.